// v2010.09.18

/*
Copyright (c) 2010 Ben Hendel-Doying

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package world;

import java.util.ArrayList;

import game.ASCII;

public final class Shop extends Scenery
{
	public static final Appearance DEFAULT_APPEARANCE = new Appearance((char)2, ASCII.COLOR_DARK_YELLOW);
	
	private int shop_level;
	private ArrayList<Item> goods = new ArrayList<Item>();
	private int next_restock = 0;
	
	public Shop(int level)
	{
		super(Demon.RandomName(), DEFAULT_APPEARANCE);
		shop_level = level;
	}
	
	// return "true" if an action is taken (if acting on the object counts as a "step")
	public boolean Act(Actor t)
	{
		return t.UseShop(this);
	}

	public int GetLevel() { return shop_level; }
	
	public boolean IsOpaque() { return false; }
	public boolean IsBlocking() { return true; }

	public void Restock()
	{
		if(location.GetWorld().GetStepCount() < next_restock)
			return;

		next_restock = location.GetWorld().GetStepCount() + 750 + World.RNG.nextInt(501);
		
		int num_goods = 3 + World.RNG.nextInt(4);
		while(goods.size() < num_goods)
		{
			Item i = location.GetWorld().CreateTreasure((int)Math.ceil(shop_level / 10.0));
			goods.add(i);
		}
	}
	
	public boolean SellItem(Item i, Actor a)
	{
		if(goods.remove(i))
		{
			a.SpendObol(i.GetValue());
			a.PickUp(i);
			return true;
		}
		else
			return false;
	}
	
	public ArrayList<Item> GetGoods() { return goods; }
	
	private static final String decline_messages[] = {
		"\"Pfhee-hee-hee!  Yeah, right!\"",
		"\"How stupid do you think I am!?\"",
		"\"Not a chance!\"",
		"\"Maybe next winter!  Hee-hee-hee-hee!\""
	};

	private static final String accept_message[] = {
		"\"It's all yours!\"",
		"\"Ha!  Pleasure doing business with you!\"",
		"\"Sure, sure!  Have at!\"",
		"\"Hee-hee!  If you insist!\"",
	};
	
	public static String DeclineMessage(String previous_message)
	{
		String m;
		
		do
		{
			m = decline_messages[World.RNG.nextInt(decline_messages.length)];
		} while(previous_message.equals(m));
		
		return m;
	}

	public static String AcceptMessage(String previous_message)
	{
		String m;
		
		do
		{
			m = accept_message[World.RNG.nextInt(accept_message.length)];
		} while(previous_message.equals(m));
		
		return m;
	}
}
